package com.jingdianjichi.subject.application.handler.subject;//package com.jingdianjichi.subject.application.handler.subject;

import com.google.common.base.Preconditions;

import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import com.jingdinajichi.subject.common.enmus.IsDeleteCodeEnum;
import com.jingdinajichi.subject.common.enmus.SubjectInfoTypeEnum;
import org.springframework.stereotype.Component;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zwy
 * @version 1.0
 * @description: RadioTypeHandler
 * @date 2025/7/13 17:48
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectRadioService subjectRadioService;
    @Resource
    private SubjectInfoService subjectInfoService;
    @Resource
    private SubjectMappingService   subjectMappingService;
    @Resource
    private SubjectLabelService subjectLabelService;
    /**
     * 枚举题目类型的识别
     *
     * @return
     */
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    /**
     *单选题目的插入
     *
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目插入
        List<SubjectRadio> subjectRadios = new ArrayList<>();
        Preconditions.checkArgument(subjectInfoBO.getSubjectOptionList() != null && subjectInfoBO.getSubjectOptionList().size() > 0, "单选题目选项不能为空");
        subjectInfoBO.getSubjectOptionList().forEach(subjectOptionBO -> {
            SubjectRadio subjectRadio = new SubjectRadio();
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setOptionType(subjectOptionBO.getOptionType());
            subjectRadio.setOptionContent(subjectOptionBO.getOptionContent());
            subjectRadio.setIsCorrect(subjectOptionBO.getIsCorrect());
            subjectRadio.setIsDeleted(IsDeleteCodeEnum.NOT_DELETE_CODE_ENUM.getCode());
            subjectRadios.add(subjectRadio);
        });
        subjectRadioService.insertBatch(subjectRadios);



    }
    /**
     * 单选题目的查询
     *
     * @param subjectInfoBO
     */
    @Override
    public SubjectInfoBO query(SubjectInfoBO subjectInfoBO) {
       //        通过题目id查询单选题表
        List<SubjectRadio> subjectRadios = subjectRadioService.queryBySubjectId(subjectInfoBO.getId());
       List<SubjectOptionBO> subjectOptionBOList = subjectRadios.stream().map(subjectRadio -> {
            SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
            subjectOptionBO.setOptionType(subjectRadio.getOptionType());
            subjectOptionBO.setOptionContent(subjectRadio.getOptionContent());
            subjectOptionBO.setIsCorrect(subjectRadio.getIsCorrect());
            return subjectOptionBO;
        }).collect(Collectors.toList());

       subjectInfoBO.setSubjectOptionList(subjectOptionBOList);
        return subjectInfoBO;
    }
}
